package com.example.mymvvmdemo.models.login.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.example.mymvvmdemo.R;
import com.example.mymvvmdemo.utils.LogUtils;
import com.example.mymvvmdemo.utils.StrUtils;
import com.example.mymvvmdemo.utils.TextColorUtils;
import com.example.mymvvmdemo.utils.ToastUtils;


public class ReadAgreementDialog {

    private Context context;
    //控件
    private Button btConfirm;
    private Button btCancel;
    private TextView tvTitle;
    //弹出框
    private Dialog dialog;

    private PromptClickListener listener;
    private boolean dismissFlag = true;

    public void setListener(PromptClickListener listener) {
        this.listener = listener;
    }

    public ReadAgreementDialog(Context context) {
        this.context = context;
        try {
            // 首先得到整个View
            View view = LayoutInflater.from(context).inflate(R.layout.dialog_read_agreement, null);

            btConfirm = view.findViewById(R.id.bt_confirm);
            btCancel = view.findViewById(R.id.bt_cancel);
            tvTitle = view.findViewById(R.id.tv_title);
            /**------------设置文本---------------------------*/

            dialog = new Dialog(context, R.style.DarkBackgroundDialog);
            dialog.setContentView(view);
            Window dialogWindow = dialog.getWindow();//获取Dialog的窗口
            //实例化窗口布局对象
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;//宽度铺满
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;//高度自适应
            lp.gravity = Gravity.CENTER;//窗口停靠在底部居中
            dialogWindow.setAttributes(lp);


            dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    if (listener != null && dismissFlag) {
                        listener.onCancelClick();
                    }
                }
            });
            btCancel.setOnClickListener(view1 -> {
                close();
                if (listener != null) {
                    listener.onCancelClick();
                }
            });
            btConfirm.setOnClickListener(view1 -> {
                close();
                if (listener != null) {
                    listener.onConfirmClick();
                }
            });

            initTextColor();

        } catch (Exception e) {
            LogUtils.e("WifiConfirmDialog error:" + e.getMessage());
        }
    }

    private void initTextColor() {

        TextColorUtils.initText(context, tvTitle,
                context.getString(R.string.login_read_all2),
                context.getString(R.string.login_user),
                context.getString(R.string.login_private),
                context.getResources().getColor(R.color.green_one), R.color.white,
                new TextColorUtils.TextColorListener() {
                    @Override
                    public void onLeftClick() {
                        if (listener != null) {
                            listener.onUserClick();
                        }
                    }

                    @Override
                    public void onRightClick() {
                        if (listener != null) {
                            listener.onPrivateClick();
                        }
                    }
                }
        );

    }

    public void setOnDismissListener(boolean flag) {
        this.dismissFlag = flag;
    }

    public void setText(int cancel, int confirm) {
        String cancelStr = context.getString(cancel);
        if (!StrUtils.isEmpty(cancelStr)) {
            btCancel.setText(cancel);
        }
        String confirmStr = context.getString(confirm);
        if (!StrUtils.isEmpty(confirmStr)) {
            btConfirm.setText(confirmStr);
        }
    }

    public void setText(String text) {
        if (!StrUtils.isEmpty(text)) {
            tvTitle.setText(text);
        }
    }

    public void setText(int resId) {
        String text = context.getString(resId);
        if (!StrUtils.isEmpty(text)) {
            tvTitle.setText(text);
        }
    }

    public void show() {
        if (dialog != null && !dialog.isShowing()) {
            dialog.show();
        }
    }

    public void close() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    public interface PromptClickListener {
        void onUserClick();

        void onPrivateClick();

        void onCancelClick();

        void onConfirmClick();
    }

}
